﻿<?php
/*
 * 上传文件 - 剪切
 */
require_once '../init.php';
define('TEMPLATE_PATH', WANYA_ROOT . '/m/view/');
$pic_type = array('gif', 'jpg', 'jpeg', 'png');

$targ_w = $targ_h = 150;
$jpeg_quality = 90;


$img_r = imagecreatefromjpeg($_FILES['head_pic']['tmp_name']);
$imgSize=getimagesize($_FILES['head_pic']['tmp_name']);
$thumb= ImageCreateTrueColor( $_POST['showW'], $_POST['showH'] );
$dst_r = ImageCreateTrueColor( $_POST['w'], $_POST['h'] );
// 等比例缩放
imagecopyresampled($thumb, $img_r, 0, 0, 0, 0,$_POST['showW'],$_POST['showH'], $imgSize[0], $imgSize[1]);

// 剪切
imagecopyresampled($dst_r,$thumb,0,0,$_POST['x'],$_POST['y'],
    $_POST['w'],$_POST['h'],$_POST['w'],$_POST['h']);


$uppath = Option::UPLOADFILE_PATH . gmdate('Ym') . '/';

$fname = substr(md5($fileName), 0, 4) . time() . '.' . $extension;

$attachpath = $uppath . $fname."jpg";

if (!is_dir(Option::UPLOADFILE_PATH)) {
    @umask(0);
    $ret = @mkdir(Option::UPLOADFILE_PATH, 0777);
    if ($ret === false) {
        return '104'; //创建文件上传目录失败
    }
}
if (!is_dir($uppath)) {
    @umask(0);
    $ret = @mkdir($uppath, 0777);
    if ($ret === false) {
        return '105'; //上传失败。文件上传目录(content/uploadfile)不可写
    }
}

// 保存
imagejpeg($dst_r,$attachpath);

echo($attachpath);

?>